package com.zenithmind.coding.pojo.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 代码提交评判结果实体类 - 遵循单一职责原则，专门处理评判结果信息
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Data
@EqualsAndHashCode(callSuper = true, onlyExplicitlyIncluded = true)
@TableName("coding_submission_result")
@Schema(description = "代码提交评判结果实体")
public class SubmissionResult extends BaseEntity {

    /**
     * 提交ID
     */
    @Schema(description = "提交ID")
    @EqualsAndHashCode.Include
    private String submissionId;

    /**
     * 运行时间（毫秒）
     */
    @Schema(description = "运行时间（毫秒）")
    private Integer runTime;

    /**
     * 运行内存（KB）
     */
    @Schema(description = "运行内存（KB）")
    private Integer runMemory;

    /**
     * 通过测试用例数
     */
    @Schema(description = "通过测试用例数")
    private Integer passedTestcases;

    /**
     * 总测试用例数
     */
    @Schema(description = "总测试用例数")
    private Integer totalTestcases;

    /**
     * 编译信息
     */
    @Schema(description = "编译信息")
    private String compileInfo;

    /**
     * 运行错误信息
     */
    @Schema(description = "运行错误信息")
    private String errorInfo;

    /**
     * 评判详情（JSON格式）
     */
    @Schema(description = "评判详情（JSON格式）")
    private String judgeDetails;

    /**
     * 代码得分
     */
    @Schema(description = "代码得分")
    private Integer score;

    /**
     * 评判开始时间
     */
    @Schema(description = "评判开始时间")
    private LocalDateTime judgeStartTime;

    /**
     * 评判结束时间
     */
    @Schema(description = "评判结束时间")
    private LocalDateTime judgeEndTime;

    /**
     * 评判耗时（毫秒）
     */
    @Schema(description = "评判耗时（毫秒）")
    private Long judgeDuration;

    /**
     * 评判机ID
     */
    @Schema(description = "评判机ID")
    private String judgeId;

    /**
     * 评判版本
     */
    @Schema(description = "评判版本")
    private String judgeVersion;

    /**
     * 编译器版本
     */
    @Schema(description = "编译器版本")
    private String compilerVersion;

    /**
     * 运行环境信息
     */
    @Schema(description = "运行环境信息")
    private String environmentInfo;

    /**
     * 测试用例执行详情（JSON格式）
     */
    @Schema(description = "测试用例执行详情（JSON格式）")
    private String testcaseDetails;

    /**
     * 重写equals方法以确保里氏替换原则
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        if (!super.equals(obj)) return false;
        
        SubmissionResult that = (SubmissionResult) obj;
        return Objects.equals(submissionId, that.submissionId);
    }

    /**
     * 重写hashCode方法以确保里氏替换原则
     */
    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), submissionId);
    }

    /**
     * 计算通过率
     */
    public Double calculatePassRate() {
        if (totalTestcases == null || totalTestcases == 0) {
            return 0.0;
        }
        if (passedTestcases == null) {
            return 0.0;
        }
        return (double) passedTestcases / totalTestcases * 100;
    }

    /**
     * 检查是否全部通过
     */
    public boolean isAllPassed() {
        return passedTestcases != null && totalTestcases != null && 
               passedTestcases.equals(totalTestcases) && totalTestcases > 0;
    }

    /**
     * 计算评判耗时
     */
    public void calculateJudgeDuration() {
        if (judgeStartTime != null && judgeEndTime != null) {
            this.judgeDuration = java.time.Duration.between(judgeStartTime, judgeEndTime).toMillis();
        }
    }

    /**
     * 检查是否超时
     */
    public boolean isTimeExceeded(Integer timeLimit) {
        return runTime != null && timeLimit != null && runTime > timeLimit;
    }

    /**
     * 检查是否内存超限
     */
    public boolean isMemoryExceeded(Integer memoryLimit) {
        return runMemory != null && memoryLimit != null && runMemory > memoryLimit;
    }
}
